<?php
class Forget extends BZ_Controller
{
    protected $type;
    protected $d;
    protected $w;
    
    public function _init()
    {
        $this->load->helper(array('email'));
        $this->load->model('user_model', 'user');
        $this->load->model('getpwd_email_model', 'getpwd_email');
        $this->d = $this->input->post();
    }
    
    /**
     * 填写账户名称
     */
    public function index()
    {
        $this->load->view('login/forget/index', $this->data);
    }
    
    //找回密码
    public function forgetPost()
    {
        if (!isset($this->d['type']) || !in_array($this->d['type'], array(1, 2, 3))) {
            $this->ajaxJson(false, '非法操作');
        }
        $this->w = array(
            'flag' => 1  
        );
        //账户确认
        if ($this->d['type'] == 1) {
            if (!isset($this->d['mobile_phone']) || !($this->d['mobile_phone'])) {
                $this->ajaxJson(false, '请输入账户名');
            }
            if (checkMobile($this->d['mobile_phone'])) {
                $this->w['mobile_phone'] = $this->d['mobile_phone'];
            } else {
                $this->w['user_name'] = $this->d['mobile_phone'];
            }
            $res = $this->user->findOne($this->w, 'mobile_phone, user_email')->row();
            if (!$res) {
                $this->ajaxJson(false, '账户名不存在');
            }
        }
        //选择方式
        if ($this->d['type'] == 2) {
            //手机
            if (isset($this->d['account']) && $this->d['account'] == 1) {
                if (!isset($this->d['mobile_phone']) || !checkMobile($this->d['mobile_phone'])) {
                    $this->ajaxJson(false, '还未绑定手机,请选择邮箱验证');
                }
                $res['mobile_phone'] = $this->d['mobile_phone'];
            } else {
                if (!isset($this->d['user_email']) || !filter_var($this->d['user_email'], FILTER_VALIDATE_EMAIL)) {
                    $this->ajaxJson(false, '还未绑定邮箱,请选择手机验证');
                }
                $res['user_email'] = $this->d['user_email'];
            }
        }
        //修改密码
        if ($this->d['type'] == 3) {
            if (!isset($this->d['verify']) || strlen($this->d['verify']) != 6) {
                $this->ajaxJson(false, '请输入正确的验证码');
            }
            if (!isset($this->d['pw']) || strlen($this->d['pw']) < 6 || strlen($this->d['pw']) > 18) {
                $this->ajaxJson(false, '请输入正确的密码');
            }
            //验证验证码
            if (isset($this->d['mobile_phone']) && checkMobile($this->d['mobile_phone'])) {
                $this->w = [
                    'mobile' => $this->d['mobile_phone'],
                    'code'   => $this->d['verify']
                ];
                $endTime = date('Y-m-d H:i:s');
                $result = $this->user_code->findOne($this->w, 'id, end_time');//, 'end_time' => $endTime
                if ($result->num_rows() <= 0) {
                    $this->ajaxJson(false, '手机验证码错误');
                }
                if ($result->row(0)->end_time < $endTime) {
                    $this->ajaxJson(false, '手机验证码超过有效期，请重新获取~');
                }
            } else {
                $this->w = array(
                    'user_email' => $this->d['user_email']
                );
                if (!$this->getpwd_email->get_data($this->d, 'id')->row()) {
                    $this->ajaxJson(false, '验证码错误');
                }
            }
            $update_data = array(
                'pw' => $this->d['pw']  
            );
            $res = $this->user->editUser($update_data, $this->w);
            if ($res === FALSE) {
                $this->ajaxJson(false, '设置密码失败');
            }
        }
        //修改成功
        $this->ajaxJson(true, '操作成功', $res);
    }
    
    //手机找回密码
    public function checkPhone()
    {
        $d = $this->input->post();
        if (!isset($d['mobile_phone']) || !checkMobile($d['mobile_phone'])) {
            $this->ajaxJson(false, '手机号码有误，请检查~');
        }

        return $this->sendCommonSms($d['mobile_phone'], 'SMS_135430021');
    }
    
    //邮箱找回密码
    public function checkEmail()
    {
        if (!isset($this->d['user_email']) || !valid_email($this->d['user_email']) ) {
            $this->ajaxJson(false, '请输入正确的邮箱地址');
        }
        $code = mt_rand(100000, 999999);
        $sendMessage = '尊敬的 '.$this->d['user_email'] .' 您好：您于 '. date('Y年m月d日 H时i分s秒') .' 申请了邮箱找回密码，验证码为：<span style="color:red;">'.$code.'</span>，有效期为10分钟，请勿向他人泄漏。';
        $this->w = array(
            'email' => $this->d['user_email']
        );
        $data = array(
            'username' => $this->userName,
            'email'    => $this->d['user_email'],
            'cdkey'    => md5($code)
        );
        $res = $this->getpwd_email->get_data($this->w, 'id')->row();
        if ($res) {
            $res = $this->getpwd_email->edit($data, $this->w);
        } else {
            $res = $this->getpwd_email->add($data);
        }
        $send = sendEmails($this->d['user_email'], '邮箱验证提醒', $sendMessage);
        if ( !$res || !$send ) {
            $this->ajaxJson(false, '邮箱发送失败');
        }
        $this->ajaxJson(true, '邮箱发送成功');
    }
}
